package com.yonyou.crm.bpub.topbar.service;

import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;

import com.yonyou.crm.base.doc.service.BaseDocBizServiceImpl;
import com.yonyou.crm.bpub.topbar.entity.*;
import com.yonyou.crm.common.enumeration.entity.EnumVO;
import com.yonyou.crm.common.enumeration.rmitf.IEnumRmService;
import com.yonyou.crm.sys.modules.service.BiztypeBizServiceImpl;

abstract public class BaseTopbar {
	
	@Autowired
	IEnumRmService enumService;
	
	@Autowired
	BiztypeBizServiceImpl biztypeService;
	
	@Autowired
	BaseDocBizServiceImpl docService;
	
	abstract public PlanVO getPresetOptions();
	abstract public String getSearchPlaceholder();
	abstract public PlanVO getSortData();
	abstract public List<SelectDataVO> getSelectData();
	
	/**
	 * 从枚举表中获取枚举值列表
	 * @param code
	 * @return
	 */
	public List<DataItemVO> getEnumList(String code) {
		List<DataItemVO> res = new ArrayList<DataItemVO>();
		String[] codes = {code};
		Map<String, List<EnumVO>> enumList = enumService.getEnumByCodes(codes);
		List<EnumVO> list =  enumList.get(code);
		if (list != null) {
			for(int i = 0; i < list.size(); i++) {
				EnumVO item = list.get(i);
				DataItemVO enumItem = new DataItemVO(item.getValue().toString(), item.getName());
				res.add(enumItem);
			}
		}
		return res;
	}
	
	/**
	 * 获取业务类型
	 * @param objName
	 * @return
	 */
	public List<DataItemVO> getBizTypeList(String objName) {
		List<DataItemVO> res = new ArrayList<DataItemVO>();
		
		objName = objName.substring(0, 1).toUpperCase() + objName.substring(1);
		Map<String, Object> biztype = biztypeService.getModuleBiztypesForCurrentRole(objName);
		
		if (biztype != null) {
			List<Map<String, Object>> biztypeList = (List<Map<String, Object>>)biztype.get("biztypeList");
			for(Map<String, Object> biz : biztypeList) {
				DataItemVO enumItem = new DataItemVO(biz.get("key").toString(), biz.get("title").toString());
				res.add(enumItem);
			}
		}
		return res;
	}
	
	/**
	 * 从档案表中获取枚举值列表
	 * @param id
	 * @return
	 */
	public List<DataItemVO> getEnumListFromDoc(String id, String name) {
		List<DataItemVO> res = new ArrayList<DataItemVO>();
		
		Map<String, Object> idsMap = new HashMap<>();
		idsMap.put(id, name);
		Map<String, Object> docList = docService.getDocAndDetailList(idsMap);

		if (docList != null) {
			List<Map<String,String>> list = (List<Map<String, String>>)docList.get(name);
			for(int i = 0; i < list.size(); i++) {
				Map<String,String> item = list.get(i);
				DataItemVO enumItem = new DataItemVO(item.get("key"), item.get("title"));
				res.add(enumItem);
			}
		}
		return res;
	}
	
}
